//
//  HeaderChooseView.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/24.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HeaderChooseView.h"
#import "HBHChouseTypeView.h"
#import "BaseBottomView.h"
@interface HeaderChooseView()
@property (nonatomic,strong)NSMutableArray *chooseArr;
@property (nonatomic,strong)BaseBottomView *chooseV;
@end
@implementation HeaderChooseView

- (instancetype)initWithFrame:(CGRect)frame TitleArr:(NSArray *)titleArr FirstArr:(NSArray *)firstArr SecondArr:(NSArray *)secondArr ChooseArr:(NSMutableArray *)chooseArr
{
    if (self = [super initWithFrame:frame]) {
        _chooseArr = [[NSMutableArray alloc]initWithArray:chooseArr];
        self.backgroundColor = [UIColor whiteColor];
        __weak typeof(self)WeakSelf = self;
        //循环创建按钮
        for (int i = 0; i < titleArr.count; i++) {
            HBHChouseTypeView *typeView = [[HBHChouseTypeView alloc]initWithFrame:CGRectMake(i*SCREENW/3, 0, 0, 0) title:titleArr[i] complete:^(NSString *title) {
                // 弹出动画
                [UIView animateWithDuration:0.3 animations:^{
                    if (_selectBlock) {
                        self.selectBlock(title);
                    }
                    //已经存在视图则去除 且改变按钮状态
                    if (_chooseV) {
                        [_chooseV.baseBgView removeFromSuperview];
                        for (UIView *subView in self.subviews) {
                            if ([subView isKindOfClass:[HBHChouseTypeView class]]) {
                                HBHChouseTypeView *typeView = (HBHChouseTypeView *)subView;
                                typeView.icon.image = [UIImage imageNamed:[typeView.title isEqualToString:title]?@"Main_search_up":@"Main_search_down"];
                            }
                        }
                    }
                } completion:^(BOOL finished) {
                    //构建新的视图
                    _chooseV = [[BaseBottomView alloc]initWithFrame:CGRectMake(0,41*screenW_6, SCREENW, SCREENH-64) KindStr:title DataArr:nil SecondArr:nil ChooseArr:_chooseArr];
                    _chooseV.chooseTapBlock = ^(NSMutableArray *chooseNowArr,NSString *title)
                    {
                        if ([title isEqualToString:@"确定"]) {
                            for (UIView *subView in WeakSelf.subviews) {
                                if ([subView isKindOfClass:[HBHChouseTypeView class]]) {
                                    HBHChouseTypeView *typeView = (HBHChouseTypeView *)subView;
                                    typeView.icon.image = [UIImage imageNamed:@"Main_search_down"];
                                }
                            }
                            [WeakSelf.chooseV.baseBgView removeFromSuperview];
                        }
                        else{
                             _chooseArr = chooseNowArr;
                        }
                    };
                }];
                
            }];
            [self addSubview:typeView];
            if (i < 2) {
                UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(typeView.frame) - .5, 0, 1, 14*screenW_6)];
                lineView.backgroundColor = backColor;
                [self addSubview:lineView];
                lineView.center = CGPointMake(lineView.center.x, self.frame.size.height/2);
            }
        }
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 41*screenW_6 - .5, SCREENW, .5)];
        lineView.backgroundColor = backColor;
        [self addSubview:lineView];
        
    }
    return self;
}


@end
